﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using EARS.App_Code;

namespace EARS
{
    public partial class EventDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string eventName = Session["EventDetails"].ToString();
                Session.Abandon();
                Event evt = new Event();
                Event eventDetails = evt.GetEvent(eventName);
                EventName.Text = eventDetails.EventName;
                StartDate.Text = eventDetails.StartDate.ToShortDateString();
                StartTime.Text = eventDetails.StartTime;
                EndDate.Text = eventDetails.EndDate.ToShortDateString();
                EndTime.Text = eventDetails.StartTime;
                Venue.Text = eventDetails.EventVenue;
                Description.Text = eventDetails.Description;
                Deadline.Text = eventDetails.EventDeadline.ToShortDateString();
                OName.Text = eventDetails.OrganizerName;
                OContact.Text = eventDetails.OrganizerContactNo.ToString();
                OEmail.Text = eventDetails.OrganizerEmail;
                if (!eventDetails.ExclusiveRole.Equals("All") && !eventDetails.ExclusiveYearOfStudy.Equals("All") && !eventDetails.ExclusiveCourse.Equals("None"))
                {
                    Exclusive.Text = eventDetails.ExclusiveRole + ", " + eventDetails.ExclusiveYearOfStudy + ", " + eventDetails.ExclusiveCourse;
                }
                else if(!eventDetails.ExclusiveRole.Equals("All") || !eventDetails.ExclusiveYearOfStudy.Equals("All") || !eventDetails.ExclusiveCourse.Equals("None"))
                {
                    if (!eventDetails.ExclusiveRole.Equals("All"))
                    {
                        Exclusive.Text = eventDetails.ExclusiveRole;
                    }
                    if (!eventDetails.ExclusiveYearOfStudy.Equals("All"))
                    {
                        if (Exclusive.Text.Length == 0)
                        {
                            Exclusive.Text = eventDetails.ExclusiveYearOfStudy;
                        }
                        else
                        {
                            Exclusive.Text = Exclusive.Text + ", " + eventDetails.ExclusiveYearOfStudy;
                        }
                    }
                    if (!eventDetails.ExclusiveCourse.Equals("None"))
                    {
                        if (Exclusive.Text.Length == 0)
                        {
                            Exclusive.Text = eventDetails.ExclusiveCourse;
                        }
                        else
                        {
                            Exclusive.Text = Exclusive.Text + ", " + eventDetails.ExclusiveCourse;
                        }
                    }
                }
                else if (eventDetails.ExclusiveRole.Equals("All") && eventDetails.ExclusiveYearOfStudy.Equals("All") && eventDetails.ExclusiveCourse.Equals("None"))
                {
                    Exclusive.Text = "nil";
                }
            }
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            Participation p = new Participation();
            Event evt = new Event();
            if (evt.CheckLimit(lblEventName.Text))
            {
                try
                {
                    bool check = p.Register(EventName.Text, User.Identity.Name);
                    if (check == false)
                    {
                        lblPopUp.Text = "Successfully Registered!";
                        mpe.Show();
                    }
                    else
                    {
                        lblPopUp.Text = "Error: You are already registered to the event.";
                        mpe.Show();
                    }
                }
                catch (SqlException ex)
                {
                    lblPopUp.Text = "Error: Unable to register for event.";
                    mpe.Show();
                }
            }
            else
            {
                lblPopUp.Text = "Error: Event has been fully registered.";
                mpe.Show();
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            mpe.Hide();
        }
    }
}